﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using HWR_Ants;
using System.Drawing;

namespace HWR_Ants.Classes
{
    class TSP_File
    {
        private string name;
        private string type;
        private string comment;
        private string dimension;
        private string edge_weight_type;
        private string[,] node_coord_section;
        private int iDimension;
        private SortedList<string, Point> coordinates;
        private const string ID_NAME = "NAME";
        private const string ID_TYPE = "TYPE";
        private const string ID_COMMENT = "COMMENT";
        private const string ID_DIMENSION = "DIMENSION";
        private const string ID_EDGE_WEIGTH_TYPE = "EDGE_WEIGHT_TYPE";
        private const string ID_NODE_COORD_SECTION = "NODE_COORD_SECTION";
        private const string ID_EOF = "EOF";

        private static string[] usedTYPES = { "TSP" };
        private static string[] usedEDGES = { "EUC_2D" };

        // NOT YET SUPPORTED
        /*
        private string edge_weight_format;
        private string display_data_type;
        private string[] edge_weight_section;
        private string[] display_data_section;
        */

        public TSP_File(string Filename)
        {
            iDimension = 0;
            readTSPFile(Filename);
            checkFile();
        }

        public SortedList<string, Point> Coordinates
        {
            get { return coordinates; }
            set { coordinates = value; }
        }

        private void checkFile()
        {
            if (!(usedTYPES.Contains<string>(type) && usedEDGES.Contains<string>(edge_weight_type)))
                resetClass();
        }

        private void resetClass()
        {
            this.comment = String.Empty;
            this.dimension = String.Empty;
            this.edge_weight_type = String.Empty;
            this.iDimension = 0;
            this.name = String.Empty;
            this.node_coord_section = null;
            this.type = String.Empty;
            throw new Exception("NOT SUPPORTED FILE...");
        }

        private void readTSPFile(string Filename)
        {
            string[] tempFileRead = File.ReadAllLines(Filename);
            for (int i = 0; i < tempFileRead.Length; i++)
            {
                string subst = String.Empty;

                if (tempFileRead[i].Contains(":"))
                    subst = tempFileRead[i].Substring(0, tempFileRead[i].IndexOf(":")).Trim();
                else
                    subst = tempFileRead[i];

                switch (subst)
                {
                    case ID_NAME:
                        name = tempFileRead[i].Substring(tempFileRead[i].IndexOf(":") + 1).Trim();
                        break;
                    case ID_TYPE:
                        type = tempFileRead[i].Substring(tempFileRead[i].IndexOf(":") + 1).Trim();
                        break;
                    case ID_COMMENT:
                        comment = tempFileRead[i].Substring(tempFileRead[i].IndexOf(":") + 1).Trim();
                        break;
                    case ID_DIMENSION:
                        dimension = tempFileRead[i].Substring(tempFileRead[i].IndexOf(":") + 1).Trim();
                        break;
                    case ID_EDGE_WEIGTH_TYPE:
                        edge_weight_type = tempFileRead[i].Substring(tempFileRead[i].IndexOf(":") + 1).Trim();
                        break;
                    case ID_NODE_COORD_SECTION:
                        try
                        {
                            iDimension = Convert.ToInt32(dimension);
                        }
                        catch
                        {
                            iDimension = 0;
                        }
                        fileReadCoordinates(tempFileRead, ref i);
                        break;
                    case ID_EOF:
                        return;
                    default:
                        break;
                }
            }

        }

        private void fileReadCoordinates(string[] tempFileRead, ref int i)
        {
            if (iDimension != 0)
            {
                node_coord_section = new string[iDimension, 3];
                coordinates = new SortedList<string, Point>();
                i++;
                for (int j = 0; j < iDimension; j++)
                {
                    string[] temp = tempFileRead[i + j].ToSeperatedString(" ");
                    node_coord_section[j, 0] = temp[0];
                    node_coord_section[j, 1] = temp[1];
                    node_coord_section[j, 2] = temp[2];
                    double x = Convert.ToDouble(temp[1].Replace(".", ","));
                    double y = Convert.ToDouble(temp[2].Replace(".", ","));
                    coordinates.Add(temp[0], new Point(Convert.ToInt32(x), Convert.ToInt32(y)));
                }
                i += iDimension;
            }
        }
    }
}
